package renxian;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class PKCard extends JLabel implements MouseListener,
        MouseMotionListener{

    Point point = null;
    Point initPoint = null;

    int value = 0;
    int type = 0;

    String name = null;
    Container pane = null;

    Spider main = null;

    boolean canMove = false;
    boolean isFront = false;
    PKCard previousCard = null;

    private boolean isHighlighted = false;

    public void mouseClicked(MouseEvent arg0){
    }

    public void flashCard(PKCard card){
        new Flash(card).start();
        if(main.getNextCard(card) != null){
            card.flashCard(main.getNextCard(card));
        }
    }

    class Flash extends Thread{
        private PKCard card = null;

        public Flash(PKCard card){
            this.card = card;
        }


        public void run(){
            boolean is = false;
            ImageIcon icon = new ImageIcon("images/white.gif");
            for (int i = 0; i < 4; i++){
                try{
                    Thread.sleep(200);
                }
                catch (InterruptedException e){
                    e.printStackTrace();
                }

                if (is){
                    this.card.turnFront();
                    is = !is;
                }
                else{
                    this.card.setIcon(icon);
                    is = !is;
                }

                card.updateUI();
            }
        }
    }


    public void mousePressed(MouseEvent mp){
        point = mp.getPoint();
        main.setNA();
        this.previousCard = main.getPreviousCard(this);

        // Clear all highlights when any card is clicked
        main.clearAllHighlights();
    }

    /**
     * 移动鼠标
     * @param mr
     */
    public void mouseReleased(MouseEvent mr){
        Point point = ((JLabel) mr.getSource()).getLocation();
        //?ж??????
        int n = this.whichColumnAvailable(point);
        if (n == -1 || n == this.whichColumnAvailable(this.initPoint)){
            this.setNextCardLocation(null);
            main.table.remove(this.getLocation());
            this.setLocation(this.initPoint);
            main.table.put(this.initPoint, this);
            return;
        }

        point = main.getLastCardLocation(n);
        boolean isEmpty = false;
        PKCard card = null;
        if (point == null){
            point = main.getGroundLabelLocation(n);
            isEmpty = true;
        }
        else{
            card = (PKCard) main.table.get(point);
        }

        if (isEmpty || (this.value + 1 == card.getCardValue())){
            point.y += 40;
            if (isEmpty) point.y -= 20;
            this.setNextCardLocation(point);
            main.table.remove(this.getLocation());
            point.y -= 20;
            this.setLocation(point);
            main.table.put(point, this);
            this.initPoint = point;
            if (this.previousCard != null){
                this.previousCard.turnFront();
                this.previousCard.setCanMove(true);
            }

            this.setCanMove(true);
        }
        else{
            this.setNextCardLocation(null);
            main.table.remove(this.getLocation());
            this.setLocation(this.initPoint);
            main.table.put(this.initPoint, this);
            return;
        }
        point = main.getLastCardLocation(n);
        card = (PKCard) main.table.get(point);
        if (card.getCardValue() == 1){
            point.y -= 240;
            card = (PKCard) main.table.get(point);
            if (card != null && card.isCardCanMove()){
                main.haveFinish(n);
            }
        }
    }


    public void setNextCardLocation(Point point){
        PKCard card = main.getNextCard(this);
        if (card != null){
            if (point == null){
                card.setNextCardLocation(null);
                main.table.remove(card.getLocation());
                card.setLocation(card.initPoint);
                main.table.put(card.initPoint, card);
            }
            else{
                point = new Point(point);
                point.y += 20;
                card.setNextCardLocation(point);
                point.y -= 20;
                main.table.remove(card.getLocation());
                card.setLocation(point);
                main.table.put(card.getLocation(), card);
                card.initPoint = card.getLocation();
            }
        }
    }


    public int whichColumnAvailable(Point point){
        int x = point.x;
        int y = point.y;
        int a = (x - 20) / 101;
        int b = (x - 20) % 101;
        if (a != 9){
            if (b > 30 && b <= 71){
                a = -1;
            }
            else if (b > 71){
                a++;
            }
        }
        else if (b > 71){
            a = -1;
        }

        if (a != -1){
            Point p = main.getLastCardLocation(a);
            if (p == null) p = main.getGroundLabelLocation(a);
            b = y - p.y;
            if (b <= -96 || b >= 96){
                a = -1;
            }
        }
        return a;
    }

    public void mouseEntered(MouseEvent arg0){
    }

    public void mouseExited(MouseEvent arg0){
    }


    public void mouseDragged(MouseEvent arg0){
        if (canMove){
            int x = 0;
            int y = 0;
            Point p = arg0.getPoint();
            x = p.x - point.x;
            y = p.y - point.y;
            this.moving(x, y);
        }
    }


    public void moving(int x, int y){
        PKCard card = main.getNextCard(this);
        Point p = this.getLocation();


        pane.setComponentZOrder(this, 1);


        main.table.remove(p);
        p.x += x;
        p.y += y;
        this.setLocation(p);
        main.table.put(p, this);
        if (card != null) card.moving(x, y);
    }

    public void mouseMoved(MouseEvent arg0){
    }


    public PKCard(String name, Spider spider){
        super();
        this.type = Integer.parseInt(name.substring(0, 1));
        this.value = Integer.parseInt(name.substring(2));
        this.main = spider;
        this.pane = this.main.getContentPane();
        this.addMouseListener(this);
        this.addMouseMotionListener(this);
        this.setIcon(new ImageIcon("images/rear.gif"));
        this.setSize(71, 96);
        this.setVisible(true);
    }


    public void turnFront(){
        this.setIcon(new ImageIcon("images/" + name + ".gif"));
        this.isFront = true;
    }

    public void turnRear(){
        this.setIcon(new ImageIcon("images/rear.gif"));
        this.isFront = false;
        this.canMove = false;
    }

    public void moveto(Point point){
        this.setLocation(point);
        this.initPoint = point;
    }


    public void setCanMove(boolean can){
        this.canMove = can;
        PKCard card = main.getPreviousCard(this);
        if (card != null && card.isCardFront()){
            if (!can){
                if (!card.isCardCanMove()){
                    return;
                }
                else{
                    card.setCanMove(can);
                }
            }
            else{
                if (this.value + 1 == card.getCardValue()
                        && this.type == card.getCardType()){
                    card.setCanMove(can);
                }
                else{
                    card.setCanMove(false);
                }
            }
        }
    }


    public boolean isCardFront(){
        return this.isFront;
    }


    public boolean isCardCanMove(){
        return this.canMove;
    }


    public int getCardValue(){
        return value;
    }

    public int getCardType(){
        return type;
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (isFront) {
            // 绘制卡牌前面
            g.setColor(Color.WHITE);
            g.fillRoundRect(0, 0, getWidth(), getHeight(), 10, 10);
            g.setColor(Color.BLACK);
            g.drawRoundRect(0, 0, getWidth(), getHeight(), 10, 10);

            // 绘制卡牌数字
            Font cardFont = new Font("Arial", Font.BOLD, 16);
            g.setFont(cardFont);
            String rankStr = String.valueOf(value);
            if (value == 1) rankStr = "A";
            else if (value == 11) rankStr = "J";
            else if (value == 12) rankStr = "Q";
            else if (value == 13) rankStr = "K";

            g.drawString(rankStr, 5, 20); // 左上角


            // 绘制中间的花色
            Font suitFont = new Font("Arial", Font.BOLD, 30);
            g.setFont(suitFont);
            String suitSymbol = "♠"; // 默认花色为黑桃
            switch (this.type) {
                case 0: suitSymbol = "♠"; break; // 黑桃
                case 1: suitSymbol = "♥"; break; // 红心
                case 2: suitSymbol = "♦"; break; // 方块
                case 3: suitSymbol = "♣"; break; // 梅花
            }
            g.drawString(suitSymbol, getWidth() / 2 - 10, getHeight() / 2);
            g.drawString(suitSymbol, getWidth()-20,20);
        } else {
            // 绘制卡牌背面
            g.setColor(new Color(176, 196, 222));
            g.fillRoundRect(0, 0, getWidth(), getHeight(), 10, 10);
            g.setColor(Color.BLACK);
            g.drawRoundRect(0, 0, getWidth(), getHeight(), 10, 10);

            // 绘制背面花纹
            g.setColor(new Color(70, 130, 180));
            for (int i = 0; i < 4; i++) {
                for (int j = 0; j < 3; j++) {
                    g.drawRect(15 + i * 12, 25 + j * 20, 10, 15);
                }
            }
        }

        if (isHighlighted) {
            // Draw red border around highlighted card
            g.setColor(Color.RED);
            g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
            g.drawRect(1, 1, getWidth() - 3, getHeight() - 3);
        }
    }

    // 新增方法：设置花色
    public void setSuit(int suit) {
        this.type = suit; // suit 0: 黑桃, 1: 红心, 2: 方块, 3: 梅花
    }

    public void setHighlight(boolean highlight) {
        isHighlighted = highlight;
        repaint();
    }
    
    public void clearHighlight() {
        isHighlighted = false;
        repaint();
    }
}
